package com.pay.service.impl;

import com.pay.entity.BankDetail;
import com.pay.entity.BankType;
import com.pay.mapper.BankDetailMapper;
import com.pay.service.IBankDetailService;
import com.pay.service.IBankTypeService;
import com.pay.utils.BasePage;
import com.pay.utils.ResponseData;

import cn.hutool.core.util.StrUtil;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Peng
 * @since 2018-12-15
 */
@Service
public class BankDetailServiceImpl extends ServiceImpl<BankDetailMapper, BankDetail> implements IBankDetailService {

	@Autowired
	private IBankTypeService bankTypeService ;
	
	
	@Override
	public void getList(BankDetail bankDetail, BasePage<BankDetail> page) {
		QueryWrapper<BankDetail> ew = new QueryWrapper<BankDetail>();
		if(bankDetail.getMerid() != null) {
			ew.eq("merid", bankDetail.getMerid()) ;
		}
		if(page.isPage()) {
			super.page(page, ew) ;
		}else {
			List<BankDetail> data = super.list(ew);
			int total = super.count(ew);
			page.setRecords(data) ;
			page.setTotal(total) ;
		}
		for(BankDetail detail : page.getRecords()) {
			BankType bankType = bankTypeService.getById(detail.getTypeid());
			detail.setBankType(bankType);
		}
	}


	@Override
	public ResponseData doBankAdd(BankDetail bankDetail) {
		if(bankDetail.getTypeid() == null) {
			return ResponseData.baseError("银行卡类型不能为空") ;
		}
		if(StrUtil.isBlank(bankDetail.getRealname())) {
			return ResponseData.baseError("开户不能为空") ;
		}
		if(StrUtil.isBlank(bankDetail.getBankcode())) {
			return ResponseData.baseError("银行卡号不能为空") ;
		}
		if(StrUtil.isBlank(bankDetail.getPhone())) {
			return ResponseData.baseError("预留手机不能为空") ;
		}
		return super.save(bankDetail) ? ResponseData.ok() : ResponseData.baseError("添加失败");
	}


	@Override
	public ResponseData doBankEdit(BankDetail bankDetail) {
		bankDetail.setMerid(null);
		if(bankDetail.getTypeid() == null) {
			return ResponseData.baseError("银行卡类型不能为空") ;
		}
		if(StrUtil.isBlank(bankDetail.getRealname())) {
			return ResponseData.baseError("开户不能为空") ;
		}
		if(StrUtil.isBlank(bankDetail.getBankcode())) {
			return ResponseData.baseError("银行卡号不能为空") ;
		}
		if(StrUtil.isBlank(bankDetail.getPhone())) {
			return ResponseData.baseError("预留手机不能为空") ;
		}
		return super.updateById(bankDetail) ? ResponseData.ok() : ResponseData.baseError("修改失败");
	}

}
